<template>
  <el-form :model="updatePasswordForm" :rules="updatePasswordRules" label-width="100px">
    <el-form-item label="旧密码" prop="oldPassword">
      <el-input v-model="updatePasswordForm.oldPassword" autocomplete="off" type="password"></el-input>
    </el-form-item>

    <el-form-item label="新密码" prop="newPassword">
      <el-input v-model="updatePasswordForm.newPassword" autocomplete="off" rules="required" type="password"></el-input>
    </el-form-item>

    <el-form-item label="确认新密码" prop="reNewPassword">
      <el-input v-model="updatePasswordForm.reNewPassword" autocomplete="off" type="password"/>
    </el-form-item>
    <!-- 根据实际情况添加 userType 输入框 -->
    <el-form-item>
      <el-button type="primary" @click="submit(updatePasswordForm)">提交</el-button>
    </el-form-item>
  </el-form>
</template>
<script lang="ts" setup>
import {reactive} from "vue";
import type {UpdatePasswordInterface} from "@/model/type";
import {updateStudentPasswordAPI} from "@/api/UserAPI";
import {ElMessage} from "element-plus";

let updatePasswordForm = reactive<UpdatePasswordInterface>({
  oldPassword: '',
  newPassword: '',
  reNewPassword: '',
  userType: '',
});
const updatePasswordRules = {
  oldPassword: [
    {required: true, message: '请输入旧密码', trigger: ['blur']},
  ],
  newPassword: [
    {required: true, message: '请输入新密码', trigger: ['blur']},
  ],
  reNewPassword: [
    {required: true, message: '请再次输入新密码', trigger: ['blur']},
    ({getFieldValue}) => ({
      validator(_, value) {
        if (!value || getFieldValue('newPassword') === value) {
          return Promise.resolve();
        }
        return Promise.reject(new Error('两次输入的新密码不一致!'));
      },
      trigger: ['blur'],
    }),
  ],
};

async function submit(updatePasswordForm: UpdatePasswordInterface) {
  updatePasswordForm.userType = localStorage.getItem("userType");
  const r: any = await updateStudentPasswordAPI(updatePasswordForm, localStorage.getItem("token"));
  if (r.code == 200) {
    ElMessage.success("修改密码成功");
  }
}
</script>

<style scoped>

</style>